const db=require('../utils/mysql')
const { v4: uuidv4 } = require('uuid');
module.exports={
    typeAdd:async (ctx,next)=>{
        let query=ctx.request.query
        let personId=uuidv4()
        let result= await new Promise((resolve,reject)=>{
            const sql=`INSERT INTO user (personId,username,password,nickname,headphoto,phone,identity,sex,type,jobnumber,entrytime) 
            values ('${personId}','${query.username}','${query.password}','${query.nickname}','${query.headphoto}','${query.phone}','${query.identity}',
            '${query.sex==undefined?'0':'0'}','${query.type}','${query.jobnumber}','${query.entrytime}')`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'人员添加成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    typeEdit:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`update user set 
            username='${query.username}',password='${query.password}',nickname='${query.nickname}',entrytime='${query.entrytime}',
            headphoto='${query.headphoto}',type='${query.type}',sex='${query.sex}',phone='${query.phone}'
            where personId='${query.personId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'人员修改成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    typeDelete:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`delete from user where personId='${query.personId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'人员删除成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    getTypeList:async (ctx,next)=>{
        let result= await new Promise((resolve,reject)=>{
            const sql=`select name  from repairer_type`
            db(sql,(err,data)=>{
                if(err) reject(err);
                let array=[]
                data.forEach(item=>{
                    array.push(item.name)
                })
                resolve({
                    msg:'工种列表获取成功',
                    status:200,
                    data:array
                })
            })
        })
        ctx.body=result

    }
}

